package inet.app;

import inet.app.controller.parser.Parser_Kqxs;
import inet.app.controller.request.Request;
import inet.app.model.object.Csd_Item;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.object.Lbt_Item;
import inet.app.model.object.LoXq_Item;
import inet.app.model.object.Lx_Item;
import inet.app.model.object.Sc_Item;
import inet.app.model.util.DateProc;
import inet.app.model.util.SmartLog;
import inet.app.view.widget.MyWidgetProvider;

import java.util.ArrayList;
import java.util.Random;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.webkit.WebView;
import android.widget.RemoteViews;

public class UpdateWidgetService extends Service{
	private static final String LOG = "xoso3mien.UpdateWidgetService";
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);

		Log.i(LOG, "Called");
		// Create some random data
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
		int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
		//UPDATE CODE
		String json = intent.getStringExtra("json");
		System.out.println("json nhận dc là ===>> " + json);
		
		//END
		ComponentName thisWidget = new ComponentName(getApplicationContext(), MyWidgetProvider.class);
		int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
		Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
		Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
		
		for (int widgetId : allWidgetIds) {
			// Create some random data
			final RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget_layout);
			// Set the text
			remoteViews.setTextViewText(R.id.tvContent, json);
			// Register an onClickListener
			Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class);

			clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

			PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			remoteViews.setOnClickPendingIntent(R.id.tvContent, pendingIntent);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
		stopSelf();
		// super.onStart(intent, startId);
	
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
